package com.dal.consumer.controller;

import lombok.extern.slf4j.Slf4j;
import com.dal.message.UserInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/users/")
@Slf4j
public class UserController {
    @Resource
    RedisTemplate redisTemplate;
    @GetMapping(value = "{name}/")
    public UserInfo getUser(@PathVariable("name")  String name) {
        log.info("name "+name);
        UserInfo user = (UserInfo) redisTemplate.opsForValue().get("users_"+name);
        log.info("user "+user);
        return user;
    }
    /**
     * 根据前缀获取所有的key
     * 例如：users_*
     */
    public Set<String> getListKey(String prefix) {
        return redisTemplate.keys(prefix.concat("*"));
    }
    @GetMapping()
    List<UserInfo> getUsers(){
        var keys=getListKey("users_");
        log.info("keys : {}",keys);
        List<UserInfo> users= redisTemplate.opsForValue().multiGet(keys);
        log.info("users : {}",users);
        return users;
    }

}
